function getSlapButton()
{
	var link_text;	 
	if (objViewer == undefined || !objViewer.isOwner )	
	{
		link_text = start_slapping_text;
	}else{
		if (ownerFriends.getById(objViewer.getId()) != undefined ){
			link_text = slap_text + ' ' + objOwner.getDisplayName();
		}else{
			link_text = start_slapping_text;
		}
	}


	var str = '<table height="21" border="0" cellpadding="0" cellspacing="0" >';
	str += '<tr>';
	str +=	'<td width="6">';
	str +=	'<img src="http://orkut.games2win.com/images/BTN_01.jpg" width="6" height="21" alt=""></td>';
	str +=	'<td background="http://orkut.games2win.com/images/BTN_02.jpg"><a href="javascript:void(0);" onclick="javascript:navigate();" class="style6"> ' + link_text + '</a></td>';
	str +=	'<td width="6">';
	str +=	 '<img src="http://orkut.games2win.com/images/BTN_04.jpg" width="6" height="21" alt=""></td>';
	str +=   '</tr>';
    str +=   '</table>';
	return str ;
}



function getParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function getProfilePic(objPerson)
{
 pic = objPerson.getField(opensocial.Person.Field.THUMBNAIL_URL) ;
		 if (pic == null || pic == "" || pic.indexOf('null') != -1 || pic.indexOf('nophoto') != -1)
		 {
		  pic = orkut_url + "/img/i_nophoto64.gif";
		 }
 return pic ;		 
}


function getTimeStamp()
{
	var currdate = new Date();
	var stamp = currdate.getYear() + currdate.getMonth() + currdate.getDate() + currdate.getHours() + currdate.getMinutes() + currdate.getSeconds() ;
		return stamp ;
}

function submitScore(aid, uid, score,uh,sh)
{
	var params = {}; 
	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT; 
	var url = 'http://orkut.games2win.com/submitscore.php?user_id=' + objViewer.getId() + '&app_id=' + aid + '&myscore=' + score + '&uh=' + uh +  '&ts=' + getTimeStamp() ; 
	//alert(url);
	gadgets.io.makeRequest(url, function(response){ postActivity(aid,score,sh); }, params);
	
}

function postActivity(aid, score, sh)
{
    if(sh=='nh'){
		sh_url='slapster2_activity_logo.gif';
	}else if(sh=='lh'){
		sh_url='slapster2_activity_logo_heart.gif';
	}else if(sh=='bg'){
		sh_url='slapster2_activity_logo_box.gif';
	}else if(sh=='hs'){
		sh_url='slapster2_activity_logo_fire.gif';
	}else if(sh=='cs'){
		sh_url='slapster2_activity_logo_ice.gif';
	}else if(sh=='kb'){
		sh_url='slapster2_activity_logo_knuckl.gif';
	}else if(sh=='ts'){
		sh_url='slapster2_activity_logo_steel.gif';
	}
		//alert(sh_url);
		activity_body = '<table  cellspacing="1px" cellpadding="1px"><tr><td valign="middle" width="10"><img src="' + getProfilePic(objViewer) +'"></td><td valign="middle" width="10"><!--<img src="http://cdn.adoption.games2win.com/sns/slapster/orkut/images/slapster_activity_logo.gif">--><img src="http://cdn.adoption.games2win.com/sns/slapster/orkut/images/' + sh_url +'" ></td><td valign="middle" width="10"><img src="' + crap_pics +'"></td><td width="5">&nbsp;</td><td valign="middle" ><strong>'+ reason_text +'</strong><br><br><a href="/Application.aspx?appId=898677835667">Start slapping now >></a></td></tr></table>'
	 var activity_params = {};
	  activity_params[opensocial.Activity.Field.TITLE] = title + ' at ' + score +  ' km/hr' ;
	  activity_params[opensocial.Activity.Field.BODY] = activity_body ;
	  var activity = opensocial.newActivity(activity_params) ;
	 opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, function(response){}) ;
	 
	 //alert(aid + "-" + score + "-" + crap_ids);
	 var params = {}; 
	 var postdata = {
    user_id : objViewer.getId(),
    friends_id : crap_ids,
	reason : reason_text,
	score : score,
	app_id : aid,
	sh : sh,
	ts : getTimeStamp()
  };
	 params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED; 
	 params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT; 
	 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
	  var url = 'http://orkut.games2win.com/submitactivity.php' 
	  gadgets.io.makeRequest(url, function(response){  }, params);
	 // resetCraps();
}

function submitUserdata(id,gender,country,profile_url)
{
	var params = {}; 
	var postdata = {
    user_id : objViewer.getId(),
    user_gender : gender,
	user_country : country,
	user_profile_url : profile_url,
	app_id : id,
	display_name : objViewer.getDisplayName(),
	th_url : getProfilePic(objViewer),
	ts : getTimeStamp()
  };
	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT; 
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
	var url = 'http://orkut.games2win.com/submituserdata.php'
	//var url = 'http://orkut.games2win.com/submituserdata.php'
	gadgets.io.makeRequest(url, function(response){}, params);
}

function getuh(id){
	//alert(id);
	var params = {}; 
	var postdata = {
    user_id : objViewer.getId(),
    ts : getTimeStamp()
  };
    params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM; 
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
	var url = 'http://orkut.games2win.com/getuh.php' ; 
//	var url = 'http://orkut.games2win.com/getscores.php' ; 
	gadgets.io.makeRequest(url, showuh, params);
	}
	
function showuh(response){
	var message = response.data.getElementsByTagName('message')
		  
	  if (message.length > 0)
	   {
		  uh="";
	   }else{
		var user = response.data.getElementsByTagName('user');
		uh = user.item(0).childNodes.item(0).firstChild.nodeValue;
		//alert(uh);
		return uh;
	  }  
}

function getTopScores(id)
{
	var params = {}; 
	var postdata = {
    user_id : objViewer.getId(),
    friends_id : friends_id,
	app_id : id,
	ctry : prefs.getCountry(),
	lang : prefs.getLang(),
	ts : getTimeStamp()
  };
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM; 
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
	var url = 'http://orkut.games2win.com/getscores.php' ; 
//	var url = 'http://orkut.games2win.com/getscores.php' ; 
	gadgets.io.makeRequest(url, showScores, params);
}

function showScores(response)
{
  if (response.data == undefined || response.data == null)
  {
	document.getElementById('friend_scores').innerHTML = "Could not fetch top scores!"
  }
  else
  {
	 var pod = response.data.getElementsByTagName('pod')
	 if (pod.length > 0)
	 {
		 showPod(pod);
	 }
	 else
	 {
		 document.getElementById('pod').innerHTML = "Could not fetch data!"
	 }
	  
	 var topscore = response.data.getElementsByTagName('topscore')
	 document.getElementById('topscore_canvas').innerHTML = topscore.item(0).firstChild.nodeValue
	  var topfriends = response.data.getElementsByTagName('friend')
	   if (topfriends.length > 0)
	 {
		 showFriendScores(topfriends);
	 }
	 else
	 {
		 document.getElementById('friend_scores').innerHTML = "Could not fetch data!"
	 }
	 
  }
}

function showPod(pod)
{
	 player_name = pod.item(0).childNodes.item(1).firstChild.nodeValue
	 player_th_url = pod.item(0).childNodes.item(2).firstChild.nodeValue
	 player_profile_url = pod.item(0).childNodes.item(3).firstChild.nodeValue
	 player_score = pod.item(0).childNodes.item(4).firstChild.nodeValue
	 var strPod = ""
	 strPod += '<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0"><tr>' ;
     strPod += '<td width="74"><img src="' + player_th_url + '" ></td>' ;
     strPod += '<td class="style2">' + player_name ;
	 strPod += '<br><span class="style3">' + score_text  + ' : ' + player_score + '</span>'  ;
     strPod += '</td></tr></table>' ;
	 document.getElementById('pod').innerHTML = strPod ;
}

function showFriendScores(response)
{
		 var strHtml = ''
		  
			strHtml += '<table width="258" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#E2EFFF">'
		   
			  var users = response;
			  //alert(users.length)
			  for (var i = 0; i < users.length ; i++)
			  {
				//alert(users.item(i).childNodes.item(0).firstChild.nodeValue)
				highscorer = viewerFriends.getById(users.item(i).childNodes.item(0).firstChild.nodeValue)
				strHtml += '<tr><td height="80">'
				strHtml += '<table width="95%" height="95" border="0" align="center" cellpadding="0" cellspacing="0"><tr>'
				strHtml += '<td width="74" height="49">'
				strHtml += '<a href="'+ highscorer.getField(opensocial.Person.Field.PROFILE_URL) +'" target="_blank">'
				strHtml += '<img src="' + getProfilePic(highscorer) + '" border="0" /></a></td>'
				strHtml += '<td valign="middle">'
				strHtml += '<a href="' + highscorer.getField(opensocial.Person.Field.PROFILE_URL) +'" target="_blank" class="style3">'
				strHtml +=  highscorer.getDisplayName() + '</a></span><br />'
				strHtml += '<span style="color: #000000; font-family:Arial, Helvetica, sans-serif; font-size:11px">' 
				strHtml +=  'Hi-score: ' + users.item(i).childNodes.item(1).firstChild.nodeValue + '</span><br />'
				strHtml += '<span style="color: #000000; font-family:Arial, Helvetica, sans-serif; font-size:11px">'
				strHtml +=  'Rank: ' + users.item(i).childNodes.item(2).firstChild.nodeValue + '</span></td>'
				strHtml += '</tr></table></td></tr><tr><td bgcolor="#D9E6F7"><img src="images/pixel.gif" width="1" height="1" /></td></tr>'
			  }
			
			  strHtml += '</table>'
		  
		  document.getElementById('friend_scores').innerHTML = strHtml;
		  gadgets.window.adjustHeight();
	    
   
}

function unhideFriendScores()
{
	document.getElementById('friend_scores').style.display = 'block' ;
	document.getElementById('global_scores').style.display = 'none' ;
}
function getGlobalScores()
{
	document.getElementById('friend_scores').style.display = 'none'
	document.getElementById('global_scores').style.display = 'block'
	
	var params = {}; 
	var postdata = {
    user_id : objViewer.getId(),
	ts : getTimeStamp()
  };
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM; 
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
	var url = 'http://orkut.games2win.com/getGlobalScores.php' ; 

	gadgets.io.makeRequest(url, showGlobalScores, params);
}

function showGlobalScores(response)
{
  
  if (response.data == undefined || response.data == null)
  {
	document.getElementById('global_scores').innerHTML = "Could not fetch top scores!"
  }
  else
  {
	  var message = response.data.getElementsByTagName('message')
		  
	  if (message.length > 0)
	   {
		  document.getElementById('global_scores').innerHTML = message.item(0).firstChild.nodeValue; 
	   }  
	   else
	   {
		 var strHtml = ''
		  
			strHtml += '<table width="258" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#759DD0">'
			
			  var users = response.data.getElementsByTagName('friend');
			  //alert(users.length)
			  for (var i = 0; i < users.length ; i++)
			  {
				//alert(users.item(i).childNodes.item(0).firstChild.nodeValue)
				//highscorer = viewerFriends.getById(users.item(i).childNodes.item(0).firstChild.nodeValue)
				strHtml += '<tr><td height="80">'
				strHtml += '<table width="95%" height="95" border="0" align="center" cellpadding="0" cellspacing="0"><tr>'
				strHtml += '<td width="74" height="49">'
				strHtml += '<a href="'+ users.item(i).childNodes.item(3).firstChild.nodeValue +'" target="_blank">'
				strHtml += '<img src="' + users.item(i).childNodes.item(2).firstChild.nodeValue+ '" border="0" /></a></td>'
				strHtml += '<td valign="middle">'
				strHtml += '<a href="' + users.item(i).childNodes.item(3).firstChild.nodeValue +'" target="_blank" class="style3">'
				strHtml +=  users.item(i).childNodes.item(1).firstChild.nodeValue + '</a></span><br />'
				strHtml += '<span style="color: #000000; font-family:Arial, Helvetica, sans-serif; font-size:11px">' 
				strHtml +=  'Hi-score: ' + users.item(i).childNodes.item(4).firstChild.nodeValue + '</span><br />'
				strHtml += '<span style="color: #000000; font-family:Arial, Helvetica, sans-serif; font-size:11px">'
				strHtml +=  'Rank: ' + users.item(i).childNodes.item(5).firstChild.nodeValue + '</span></td>'
				strHtml += '</tr></table></td></tr><tr><td bgcolor="#D9E6F7"><img src="images/pixel.gif" width="1" height="1" /></td></tr>'
			  }
			
			  strHtml += '</table>'
		  
		  document.getElementById('global_scores').innerHTML = strHtml;
		  gadgets.window.adjustHeight();
	   }	  
   }
}